<?php
$noSeasonChange = true;
$title = "Profile";
include ("includes/header.php");
$submit = getParam("submit");
if (isset ($submit)) {
    $coach_id = getParam("coach_id");
    $name = getParam("name");
    $password = getParam("password");
    $email = getParam("email");
    $type = getParam("type");
    if (isset ($loggedin) && ($loggedin->isAdmin() or $loggedin->id == $coach_id)) {
        if ($loggedin->isAdmin()) {
            if (isset ($coach_id)) {
                $query = "update coaches set name='$name', password='$password', type=$type, email='$email' where coach_id=$coach_id";
            } else {
                $query = "insert into coaches (name, password, type) values ('$name', '$password', $type)";
            }
        } else {
            $query = "update coaches set name='$name', password='$password', email='$email' where coach_id=$coach_id";
        }

        $rs = executeQuery($query);
        if ($rs) {
            echo "<br />Updated<br />";
        }
    } else {
        echo "<br />You must be an administrator to modify the coaches<br />";
    }
}

$coaches = loadCoaches();

echo<<< EOQ
<caption><h3>User Profiles</h3></caption>
<tr>
  <th>Name</th>
  <th>Password</th>
  <th>Email</th>
EOQ;
if ($loggedin->isAdmin()) {
    echo<<< EOQ
  <th>Type</th>
EOQ;
}
echo<<< EOQ
  <th>&nbsp;</th>
</tr>

EOQ;
foreach ($coaches as $coach) {
    if (isset ($loggedin)) {
        if ($loggedin->isAdmin() or $coach->id == $loggedin->id) {
            echo<<< EOQ
<tr>
  <form method="post" action="coach_editor.php">
    <input type="hidden" name="coach_id" value="$coach->id">
    <td><input type="text" size="24" maxsize="64" name="name" value="$coach->name"></td>
    <td><input type="text" size="24" maxsize="64" name="password" value="$coach->password"></td>
    <td><input type="text" size="24" maxsize="64" name="email" value="$coach->email"></td>
EOQ;
            if ($loggedin->isAdmin()) {
                echo<<< EOQ
    <td>
      <select name="type">
        <option value="0">Normal</option>
        <option value="255"
EOQ;
                if ($coach->isAdmin()) {
                    echo "selected";
                }
                echo<<< EOQ
>Administrator</option>
      </select>
    </td>
EOQ;
            }
            echo<<< EOQ
    <td><input type="submit" name="submit" value="Update"></td>
  </form>
</tr>
EOQ;
        }
    }
}
if (isCommissioner()) {
    echo<<< EOQ
<tr>
  <form method="post" action="coach_editor.php">
    <td><input type="text" size="24" maxsize="64" name="name" value=""></td>
    <td><input type="text" size="24" maxsize="64" name="password" value=""></td>
    <td>
      <select name="type">
        <option value="0">Normal</option>
        <option value="255">Administrator</option>
      </select>
    </td>
    <td align="center"><input type="submit" name="submit" value="Add"></td>
  </form>
</tr>
EOQ;
}

include ("includes/footer.php");
?>